/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.server.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.feiyue.basic.server.parser.Book;
import java.util.ArrayList;
import java.util.List;

public class BookDBHandler {
	public static final String COLUMN_author = "author";
	public static final String COLUMN_categoryid = "categoryid";
	public static final String COLUMN_coveraddr = "coveraddr";
	public static final String COLUMN_coverid = "coverid";
	public static final String COLUMN_id = "id";
	public static final String COLUMN_introduction = "introduction";
	public static final String COLUMN_issuestate = "issuestate";
	public static final String COLUMN_name = "name";
	public static final String CREATE_TABLE_booklist = "CREATE TABLE IF NOT EXISTS booklist (categoryid INTEGER not null, id INTEGER PRIMARY KEY not null, name text not null, author text not null, coveraddr text not null, introduction text not null, issuestate text not null, coverid INTEGER not null);";
	public static final String myTableName = "booklist";
	protected SQLiteDatabase mSQLiteDatabase = null;

	public BookDBHandler(Context paramContext) throws Exception {
		this.mSQLiteDatabase = paramContext.openOrCreateDatabase("novel", 0,
				null);
		this.mSQLiteDatabase
				.execSQL("CREATE TABLE IF NOT EXISTS booklist (categoryid INTEGER not null, id INTEGER PRIMARY KEY not null, name text not null, author text not null, coveraddr text not null, introduction text not null, issuestate text not null, coverid INTEGER not null);");
	}

	private void insert(Book paramBook, Context paramContext) {
		if (isExist(paramBook))
			return;
		String str = "insert into booklist values(" + paramBook.getCategoryId()
				+ "," + paramBook.getId() + ", \""
				+ paramBook.getName().replace("\"", "") + "\", \""
				+ paramBook.getAuthor().replace("\"", "") + "\", \""
				+ paramBook.getCoverAddr() + "\", \""
				+ paramBook.getIntroduction().replace("\"", "") + "\"" + ","
				+ " \"" + paramBook.getIssuestate() + "\", "
				+ paramBook.getCoverid() + ");";
		this.mSQLiteDatabase.execSQL(str);
	}

	private boolean isExist(Book paramBook) {
		String str = "select * from booklist where id = " + paramBook.getId();
		Cursor localCursor = null;
		try {
			localCursor = this.mSQLiteDatabase.rawQuery(str, null);
			if (localCursor != null && localCursor.moveToFirst()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (localCursor != null) {
				localCursor.close();
			}
		}
		return false;
	}

	public void destroy() {
		if (this.mSQLiteDatabase == null)
			return;
		this.mSQLiteDatabase.close();
	}

	public List<Book> getBookList(Context paramContext, int paramInt) {
		ArrayList localArrayList = new ArrayList();
		String str1 = "select * from booklist where categoryid = " + paramInt;
		
		Cursor localCursor = null;
		try {
			localCursor = this.mSQLiteDatabase.rawQuery(str1, null);
			if (localCursor != null && localCursor.moveToFirst())
				do {
					Book localBook = new Book();
					String str2 = localCursor.getString(localCursor
							.getColumnIndex("coveraddr"));
					String str3 = localCursor.getString(localCursor
							.getColumnIndex("name"));
					int i = localCursor
							.getInt(localCursor.getColumnIndex("id"));
					String str4 = localCursor.getString(localCursor
							.getColumnIndex("author"));
					String str5 = localCursor.getString(localCursor
							.getColumnIndex("introduction"));
					int j = localCursor.getInt(localCursor
							.getColumnIndex("categoryid"));
					int k = localCursor.getInt(localCursor
							.getColumnIndex("coverid"));
					String str6 = localCursor.getString(localCursor
							.getColumnIndex("issuestate"));
					localBook.setCoverAddr(str2);
					localBook.setName(str3);
					localBook.setId(i);
					localBook.setAuthor(str4);
					localBook.setIntroduction(str5);
					localBook.setIssuestate(str6);
					localBook.setCategoryId(j);
					localBook.setCoverid(k);
					localArrayList.add(localBook);
				} while (localCursor.moveToNext());
			return localArrayList;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (localCursor != null) {
				localCursor.close();
			}
		}
		return null;
	}

	//TODO 不一定正确，查询结果是否有条件过滤？
	public List<Book> getBookList(Context paramContext, int paramInt1,
			int paramInt2, int paramInt3) {
		ArrayList localArrayList = new ArrayList();
		String str1 = "select * from booklist where categoryid = " + paramInt1
				+ " and id > " + paramInt2;
			Cursor localCursor = null;
			try {
				localCursor = this.mSQLiteDatabase.rawQuery(str1, null);
				int i = 0;
				if (localCursor != null && localCursor.moveToFirst()) {
					do {
						Book localBook = new Book();
						String str2 = localCursor.getString(localCursor
								.getColumnIndex("coveraddr"));
						String str3 = localCursor.getString(localCursor
								.getColumnIndex("name"));
						int j = localCursor
								.getInt(localCursor.getColumnIndex("id"));
						String str4 = localCursor.getString(localCursor
								.getColumnIndex("author"));
						String str5 = localCursor.getString(localCursor
								.getColumnIndex("introduction"));
						int k = localCursor.getInt(localCursor
								.getColumnIndex("categoryid"));
						int l = localCursor.getInt(localCursor
								.getColumnIndex("coverid"));
						String str6 = localCursor.getString(localCursor
								.getColumnIndex("issuestate"));
						localBook.setCoverAddr(str2);
						localBook.setName(str3);
						localBook.setId(j);
						localBook.setAuthor(str4);
						localBook.setIntroduction(str5);
						localBook.setIssuestate(str6);
						localBook.setCategoryId(k);
						localBook.setCoverid(l);
						localArrayList.add(localBook);
					} while (localCursor.moveToNext());
				}
				return localArrayList;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (localCursor != null) {
					localCursor.close();
				}
			}
			
			return null;
	}

	//TODO 是否正确？
//	public List<Book> getBookList(Context paramContext, String paramString) {
//		String str1 = "select * from booklist where";
//		ArrayList localArrayList;
//		if (paramString.length() <= 2) {
//			str1 = str1 + " name like '%" + paramString.replace("\"", "")
//					+ "%'";
//			localArrayList = new ArrayList();
//		}
//		try {
//			Cursor localCursor = this.mSQLiteDatabase.rawQuery(str1, null);
//			if (localCursor.moveToFirst())
//				do {
//					Book localBook = new Book();
//					String str3 = localCursor.getString(localCursor
//							.getColumnIndex("coveraddr"));
//					String str4 = localCursor.getString(localCursor
//							.getColumnIndex("name"));
//					int j = localCursor
//							.getInt(localCursor.getColumnIndex("id"));
//					String str5 = localCursor.getString(localCursor
//							.getColumnIndex("author"));
//					String str6 = localCursor.getString(localCursor
//							.getColumnIndex("introduction"));
//					int k = localCursor.getInt(localCursor
//							.getColumnIndex("categoryid"));
//					int l = localCursor.getInt(localCursor
//							.getColumnIndex("coverid"));
//					String str7 = localCursor.getString(localCursor
//							.getColumnIndex("issuestate"));
//					localBook.setCoverAddr(str3);
//					localBook.setName(str4);
//					localBook.setId(j);
//					localBook.setAuthor(str5);
//					localBook.setIntroduction(str6);
//					localBook.setIssuestate(str7);
//					localBook.setCategoryId(k);
//					localBook.setCoverid(l);
//					localArrayList.add(localBook);
//				} while (localCursor.moveToNext());
//			localCursor.close();
//			return localArrayList;
////			String str2;
////			for (int i = 0;; ++i) {
////				if (i < -1 + paramString.length())
////					;
////				str2 = paramString.replace("\"", "").substring(i, i + 2);
////				if (i != 0)
////					break;
////				str1 = str1 + " name like '%" + str2 + "%'";
////			}
////			str1 = str1 + " or name like '%" + str2 + "%'";
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return null;
//	}

	public void insert(List<Book> paramList, Context paramContext) {
		if (paramList == null)
			return;
		this.mSQLiteDatabase.beginTransaction();
		int i = paramList.size();
		for (int j = 0;; ++j) {
			if (j >= i) {
				this.mSQLiteDatabase.setTransactionSuccessful();
				this.mSQLiteDatabase.endTransaction();
				return;
			}
			insert((Book) paramList.get(j), paramContext);
		}
	}
}